﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Company.Shell.Controls
{
    /// <summary>
    /// Fan.xaml 的交互逻辑
    /// </summary>
    public partial class Fan : UserControl
    {
        private Brush redBrush = new SolidColorBrush(Color.FromRgb(203, 22, 22));
        private Brush openBrush = new SolidColorBrush(Color.FromRgb(75, 169, 69));
        private Brush closeBrush;
        public Fan()
        {
            InitializeComponent();
            closeBrush = fanBlade1.Fill;
        }

        private DoubleAnimation animation1 = new DoubleAnimation()
        {
            From = 0,
            To = 360,
            Duration = TimeSpan.FromSeconds(2),
            RepeatBehavior = RepeatBehavior.Forever,
        };

        private DoubleAnimation animation2 = new DoubleAnimation()
        {
            From = 120,
            To = 360 + 120,
            Duration = TimeSpan.FromSeconds(2),
            RepeatBehavior = RepeatBehavior.Forever,
        };

        private DoubleAnimation animation3 = new DoubleAnimation()
        {
            From = 240,
            To = 360 + 240,
            Duration = TimeSpan.FromSeconds(2),
            RepeatBehavior = RepeatBehavior.Forever,
        };

        /// <summary>
        /// 风机工作状态
        /// </summary>
        public int State
        {
            get { return (int)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }

        public static readonly DependencyProperty StateProperty =
            DependencyProperty.Register("State", typeof(int), typeof(Fan), new PropertyMetadata(0, new PropertyChangedCallback(StateChangedCallback)));

        private static void StateChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (Fan)d;
            if (control == null)
                return;
            if (e.NewValue is int state)
            {
                //state = 0,灰色，停止
                //state = 1,绿色，转动
                //state = 2,红色，停止

                if (state == 0)
                {
                    control.fanBlade1.Fill = control.closeBrush;
                    control.fanBlade2.Fill = control.closeBrush;
                    control.fanBlade3.Fill = control.closeBrush;
                    control.fanBlade1.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade2.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade3.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                }
                else if (state == 1)
                {
                    control.fanBlade1.Fill = control.openBrush;
                    control.fanBlade2.Fill = control.openBrush;
                    control.fanBlade3.Fill = control.openBrush;
                    control.fanBlade1.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, control.animation1);
                    control.fanBlade2.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, control.animation2);
                    control.fanBlade3.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, control.animation3);
                }
                else if (state == 2)
                {
                    control.fanBlade1.Fill = control.redBrush;
                    control.fanBlade2.Fill = control.redBrush;
                    control.fanBlade3.Fill = control.redBrush;
                    control.fanBlade1.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade2.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade3.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                }
                else
                {
                    control.fanBlade1.Fill = control.closeBrush;
                    control.fanBlade2.Fill = control.closeBrush;
                    control.fanBlade3.Fill = control.closeBrush;
                    control.fanBlade1.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade2.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                    control.fanBlade3.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, null);
                }
            }
        }
    }
}
